package com.xinzhi.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinzhi.pojo.LoanApplication;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface LoanApplicationMapper {

    @Select("SELECT * FROM loan_application WHERE Dk_sqid = #{applicationId}")
    LoanApplication getLoanApplicationById(@Param("applicationId") Integer applicationId);

    @Update("UPDATE loan_application " +
            "SET Sqrq = #{applicationDate}, Sprid = #{approverId}, Fdbz = #{loanFlag} " +
            "WHERE Dk_sqid = #{applicationId}")
    void updateLoanApplication(@Param("applicationId") Integer applicationId,
                               @Param("applicationDate") Date applicationDate,
                               @Param("approverId") Integer approverId,
                               @Param("loanFlag") Integer loanFlag);

    @Update("UPDATE loan_application " +
            "SET Dk_sqh = #{applicationNumber}, " +
            "Dkkhid = #{userId}, " +
            "Dkzl = #{loanType}, " +
            "Dkfw = #{loanScope}, " +
            "Dbrid = #{guarantorId}, " +
            "Dkje = #{loanAmount}, " +
            "Sqrq = #{applicationDate}, " +
            "Sprq = #{approvalDate}, " +
            "Fdbz = #{loanFlag}, " +
            "Sprid = #{approverId}, " +
            "Dkzh = #{loanAccount}, " +
            "Hkfs = #{repaymentMethod}, " +
            "Mqhkje = #{monthlyRepayment}, " +
            "dcxdyid = #{investigatorId} " +
            "WHERE Dk_sqid = #{id}")
    void updateById(LoanApplication loanApplication);

    @Select("SELECT * FROM loan_application WHERE Dk_sqh = #{ew.customSqlSegment}")
    LoanApplication selectOne(QueryWrapper<LoanApplication> dkSqh);

    @Delete("DELETE FROM loan_application WHERE Dk_sqid = #{id}")
    void deleteById(Integer id);

    @Insert("INSERT INTO loan_application (Dk_sqh, Dkkhid, Dkzl, Dkfw, Dbrid, Dkje, Sqrq, " +
            "Sprq, Fdbz, Sprid, Dkzh, Hkfs, Mqhkje, dcxdyid) " +
            "VALUES (#{applicationNumber}, #{userId}, #{loanType}, #{loanScope}, #{guarantorId}, " +
            "#{loanAmount}, #{applicationDate}, #{approvalDate}, #{loanFlag}, #{approverId}, " +
            "#{loanAccount}, #{repaymentMethod}, #{monthlyRepayment}, #{investigatorId})")
    void insert(LoanApplication loanApplication);

    @Select("SELECT * FROM loan_application WHERE Dk_sqid = #{id}")
    LoanApplication selectById(Integer id);

    @Select("SELECT * FROM loan_application")
    List<LoanApplication> selectList(Object o);
}
